package com.ezonsport.android.webservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.ezonsport.android.model.User;

public class WSGetUser {
	private static final String TAG = "WSGetUser";
	private String userName="";
	final static String webServiceNameSpace="http://IwebService.iservice.domain/";
	//final static String webServiceURL="http://10.136.2.69:8099/EzonSport/services/UserManagement";
	final static String webServiceURL="http://10.0.2.2:8080//services/UserManagement";
	
	final static String soapAction="http://IwebService.iservice.domain/GetUserInfo";
	//�����û�����
	private User user=null;
	//���ڱ���webservice���ص���Ϣ
	private List<String> messageList=new ArrayList<String>();
	public WSGetUser(String username){
		this.userName=username;
	}
	

	//���ںͷ�������¶��webserviceͨ��
	public void talkWithWeb(){
		String methodName="GetUserInfo";
		//���ɵ�
		HttpTransportSE ht=new HttpTransportSE(webServiceURL);
		//����������ʹ(���غͷ������˵�ͨ����Ҫ�����)
		SoapSerializationEnvelope envelop=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		//��Ϣ��
		SoapObject soapObject=new SoapObject(webServiceNameSpace, methodName);
		//������Ϣ��Ĳ���
		soapObject.addProperty("in0", userName);
		//ͨ��׼��,װ��
		envelop.bodyOut=soapObject;
		envelop.setOutputSoapObject(soapObject);
		//���濪ʼ���Ǻ�webservice��ͨ��	
		try {
			//����webservice
			//envelop.dotNet=true;
			ht.call(soapAction, envelop);
			if(envelop.getResponse()!=null){
				//��ȡ��������Ӧ���ص�soap��Ϣ
				SoapObject result=(SoapObject) envelop.bodyIn;
				//����������
				SoapObject detail= (SoapObject) result.getProperty("out");
				//User d= (User) result.getProperty("out");
				//messageList.add(d.getName());
				parseSoap(detail);
				
			}else{
				messageList.add("no data");
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.d(null, e.getMessage());
		}
	}
	//�����ص���
	public List<String> getMessage(){
		talkWithWeb();
		return messageList;
	}
	
	//����soap����,װ�����
	private void parseSoap(SoapObject soapObject){
		messageList.add(soapObject.getProperty("brithday").toString());
		messageList.add(soapObject.getProperty("height").toString());
		messageList.add(soapObject.getProperty("pwd").toString());
		messageList.add(soapObject.getProperty("sex").toString());
		messageList.add(soapObject.getProperty("standardWeight").toString());
		messageList.add(soapObject.getProperty("userID").toString());
		messageList.add(soapObject.getProperty("userName").toString());
	}
	
	//��ȡ�û�User����
	public User getUser(){
		user =new User();
		Log.v(TAG, String.valueOf(messageList.size()));
		String time=messageList.get(0);
		int year=Integer.parseInt(time.substring(0, 4));
		int month=Integer.parseInt(time.substring(5, 7));
		int day=Integer.parseInt(time.substring(8, 10));
		//String []ymd=messageList.get(0).split("-");
		Date brithday=new Date(year,month,day);
		user.setBrithday(brithday);
		user.setHeight(Double.parseDouble(messageList.get(1)));
		user.setGender(messageList.get(3));
		user.setStandardWeight(Double.parseDouble(messageList.get(4)));
		user.setId(Integer.parseInt(messageList.get(5)));
		user.setName(userName);
		return user;
	}
}
